<?
/**
* @desc Класс-обертка для ленивой работы с CURL
*/
class Curl{
    public $userpwd = "";
    public $post = "";
    public $referer = "";
    public $savecookie = false;
    private $tempFile = "";
    private $ch; //идентификатор curl

	/**
    * @desc статическая функция для сверх ленивого использования
    */
    public static function getHtml($url, $userpwd = "", $post = "", $referer = "", $savecookie = false){
        $curl = new Curl();
        $curl->userpwd = $userpwd;
        $curl->post = $post;
        $curl->referer = $referer;
		$curl->savecookie = $savecookie;
        $res = $curl->get($url);
        return $res;
	}
    
    function __construct(){
        // инициализация сеанса
        $this->ch = curl_init();
    }
    
    function __destruct(){
        // завершение сеанса и освобождение ресурсов
        curl_close($this->ch);
        if($this->tempFile) unlink($this->tempFile);
    }
    
    /*
    * Функция возвращает Html указанной страницы
    */
    public function get($url, $pattern = false){
        // установка URL и других необходимых параметров
        curl_setopt($this->ch, CURLOPT_HEADER, 0);
        curl_setopt($this->ch, CURLOPT_ENCODING, "");
        curl_setopt($this->ch, CURLOPT_RETURNTRANSFER,1);
        curl_setopt($this->ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");
        curl_setopt($this->ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        if($this->userpwd)
            curl_setopt($this->ch, CURLOPT_USERPWD, $this->userpwd);
        if($this->post){
            curl_setopt($this->ch, CURLOPT_POST, 1);
            curl_setopt($this->ch, CURLOPT_POSTFIELDS, $this->post);
        }
        if($this->referer){
            curl_setopt($this->ch, CURLOPT_REFERER, $this->referer);
        }
        
        if($this->savecookie){
            if(!$this->tempFile) $this->tempFile = tempnam (/*dirname(realpath(__FILE__))*/"", "COOK_");            
            curl_setopt($this->ch, CURLOPT_COOKIEJAR, $this->tempFile);
            curl_setopt($this->ch, CURLOPT_COOKIEFILE, $this->tempFile);            
        }
        curl_setopt($this->ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($this->ch, CURLOPT_SSL_VERIFYHOST, 0);
        if(is_callable("Puny::e")){
        	$url = mb_strtolower($url);
        	if(!preg_match("/http(s)?:\/\//i", $url)) $url = 'http://'.$url;
        	$parts = parse_url($url);
         	$url = (!empty($parts['scheme']) ? $parts['scheme'] : "http")
         	."://".Puny::e(!empty($parts['host']) ? $parts['host'] : "")
         	.(!empty($parts['path']) ? $parts['path'] : "/")
         	.(!empty($parts['query']) ? "?".$parts['query'] : "")
         	.(!empty($parts['fragment']) ? "#".$parts['fragment'] : "");
		} 
        curl_setopt($this->ch, CURLOPT_URL, $url);

        // загрузка страницы 
        $res = curl_exec($this->ch);
		
		if($pattern){
			preg_match($pattern, $res, $matches);
			if(!count($matches)) return false;
			array_shift($matches);
			if(count($matches) == 1) return $matches[0];
			return $matches;
		}
        return $res;
    }
    
    public function getInfo(){
        return curl_getinfo($this->ch);
    }
    
    public function getCharset(){
        $info = $this->getInfo();
        if(empty($info['content_type'])) return;
        $pare = explode("charset=", $info['content_type']);
        if(empty($pare[1])) return;
        return $pare[1];
    }
}
?>